home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 744 / nfd / nfd.p < prev    next >
Text File  |  1995-03-18  |  7KB  |  172 lines

  1. {
  2.     +------------------------------------------------------+
  3.     |NFD (Newest File Data) © (copyright) 1991 Jon Maxwell |
  4.     +------------------------------------------------------+
  5.     |This program may be freely distributed, but I reserve |
  6.     |all rights to it and to the source code.              |
  7.     +------------------------------------------------------+
  8.     |USAGE: NFD [±Pattern] [Directory] [Command]           |
  9.     |  [Command] is the line to be run with the newest file|
  10.     |name, in this form: (Text)¿(Text) where (Text) is part|
  11.     |of the command and ¿ (shift-alt-M) determines where   |
  12.     |the file name will be inserted.                       |
  13.     |  a + pattern means that it must match that pattern, a|
  14.     |- means that it must not match.                       |
  15.     +------------------------------------------------------+
  16.     |  If there is no [Command] parameter, NFD will assume |
  17.     |that you have ARP and will set an ARP global variable |
  18.     |under the name of $FileName.                          |
  19.     +------------------------------------------------------+
  20.     |Source compiled with Patrick Quaid's PCQ              |
  21.     +------------------------------------------------------+
  22. }
  23. PROGRAM LastFileWrittenName (input,output);
  24. {$I "Include:Libraries/DOS.i"}
  25. {$I "Include:Utils/StringLib.i"}
  26. {$I "Include:Utils/Parameters.i"}
  27. VAR
  28.   FIB:FileInfoBlockPtr;
  29.   FL:FileLock;
  30.   I,
  31.   Pos,
  32.   Minutes,
  33.   PatternType,
  34.   Days:Integer;
  35.   BestDate:DateStampPtr;
  36.   DirName,
  37.   Pattern,
  38.   BestName,
  39.   FinalName,
  40.   TempStr,
  41.   CmdLine:String;
  42.   QuoteMode,
  43.   PatternResult:Boolean;
  44.  
  45.  
  46. FUNCTION PatternCheck (Str,Pat:String):Boolean;
  47. VAR
  48.   StrPos,
  49.   PatPos:Integer;
  50. BEGIN
  51.   StrPos:=0;
  52.   PatPos:=0;
  53.   WHILE StrPos<strlen(Str) DO BEGIN
  54.     IF ToUpper(Str[StrPos])=ToUpper(Pat[PatPos]) THEN
  55.         Inc(PatPos)
  56.       ELSE
  57.         PatPos:=0;
  58.     IF PatPos=strlen(Pat) THEN PatternCheck:=TRUE;
  59.     Inc(StrPos);
  60.   END;
  61.   PatternCheck:=FALSE;
  62. END;
  63.  
  64.  
  65. PROCEDURE StrCpyII(Dest,Source:String;Start,Finish:Integer);
  66. VAR Loop:Integer;
  67.     DPos:Integer;
  68. BEGIN
  69.    DPos:=0;
  70.    FOR Loop:=Start TO Finish DO BEGIN
  71.       Dest[DPos]:=Source[Loop];
  72.       Inc(DPos);
  73.    END;
  74.    Dest[DPos]:='';
  75. END;
  76.  
  77.  
  78. PROCEDURE WriteInfo;
  79. CONST
  80.   NumLines=18;
  81.   Lines:Array [1..NumLines] OF String=(
  82.     "+-------------------------------------------------------+",
  83.     "|NFD (Newest File Data) © (copyright) 1991 Jon Maxwell  |",
  84.     "+-------------------------------------------------------+",
  85.     "|This program may be freely distributed, but I reserve  |",
  86.     "|all rights to it and to the source code.               |",
  87.     "+-------------------------------------------------------+",
  88.     "|USAGE: NFD [±Pattern] [Directory] [Command]            |",
  89.     "|  [Command] is the line to be run with the newest file |",
  90.     "|name, in this form: (Text)¿(Text) where (Text) is part |",
  91.     "|of the command and ¿ (shift-alt-M) determines where    |",
  92.     "|the file name will be inserted.                        |",
  93.     "|  a + pattern means that it must match that pattern, a |",
  94.     "|- means that it must not match.                        |",
  95.     "+-------------------------------------------------------+",
  96.     "|  If there is no [Command] parameter, NFD will assume  |",
  97.     "|that you have ARP and will set an ARP global variable  |",
  98.     "|under the name of $FileName.                           |",
  99.     "+-------------------------------------------------------+");
  100. VAR
  101.   Loop:Integer;
  102. BEGIN
  103.   FOR Loop:=1 TO NumLines DO
  104.     writeln(Lines[Loop]);
  105.   Exit(0);
  106. END;
  107.  
  108. BEGIN
  109.   IF CommandLine[0]='?' THEN WriteInfo; {\              }
  110.   PatternType:=0;                       { \             }
  111.   BestName:=AllocString(200);           {  \            }
  112.   DirName:=AllocString(200);            {   \           }
  113.   FinalName:=AllocString(200);          {    »Sets the  }
  114.   CmdLine:=AllocString(200);            {   / variables }
  115.   new(FIB);new(BestDate);               {  /            }
  116.   BestDate^.ds_Days:=0;                 { /             }
  117.   BestDate^.ds_Minute:=0;               {/              }
  118.  
  119.   GetParam(1,CmdLine);
  120.   IF (CmdLine[0]='+') OR (CmdLine[0]='-') THEN BEGIN
  121.     Pattern:=AllocString(100);
  122.     StrCpyII(Pattern,CmdLine,1,strlen(CmdLine));
  123.     IF CmdLine[0]='+' THEN PatternType:=1 ELSE PatternType:=2;
  124.     GetParam(2,CmdLine);
  125.   END;
  126.   strcpy(DirName,CmdLine);
  127.   FL:=Lock(CmdLine,ACCESS_READ);
  128.   IF FL=nil THEN BEGIN writeln("Couldn't get a lock on: ",CmdLine," directory!"); Exit(20); END;
  129.   IF NOT Examine(FL,FIB) THEN BEGIN UnLock(FL); Exit(20); END;
  130.   WHILE ExNext(FL,FIB)=TRUE DO BEGIN
  131.     Days:=FIB^.fib_Date.ds_Days;
  132.     Minutes:=FIB^.fib_Date.ds_Minute;
  133.     PatternResult:=PatternCheck(string(@FIB^.fib_FileName[0]),Pattern);
  134.     IF (FIB^.fib_DirEntryType<0) AND ((Days>BestDate^.ds_Days) OR ((Days=BestDate^.ds_Days) AND (Minutes>BestDate^.ds_Minute))) THEN
  135.       IF (PatternType=0) OR ((PatternType=1) AND (PatternResult)) OR ((PatternType=2) AND NOT (PatternResult)) THEN BEGIN
  136.            BestDate^.ds_Days:=Days;
  137.            BestDate^.ds_Minute:=Minutes;
  138.            strcpy(BestName,string(@FIB^.fib_FileName[0]) );
  139.       END;
  140.   END;
  141.   strcat(FinalName,"FileName=");
  142.   strcat(FinalName,DirName);
  143.   I:=strlen(DirName)-1;
  144.   IF (DirName[I]<>'/') AND (DirName[I]<>':') THEN strcat(FinalName,"/");
  145.   strcat(FinalName,BestName);
  146.   IF strlen(BestName)=0 THEN strcpy(FinalName,"!");
  147.   IF PatternType=0 THEN GetParam(2,CmdLine) ELSE GetParam(3,CmdLine);
  148.   IF CmdLine[0]='' THEN  {ARP Section: Defines variable $FileName as the filename}
  149.     BEGIN IF Execute(FinalName,FileHandle(0),FileHandle(0)) THEN; Exit(0); END
  150.    ELSE BEGIN
  151.     IF strlen(BestName)=0 THEN BEGIN writeln("No match for the pattern or no files in the directory!"); Exit(0); END;
  152.     TempStr:=AllocString(500);  {nonARP Section: Gets 2nd parameter &}
  153.                                 {makes an executable string with the }
  154.     Pos:=strpos(CmdLine,'¿');   {Filename and the parameter          }
  155.     QuoteMode:=FALSE;
  156.     IF strpos(BestName,' ')>-1 THEN QuoteMode:=TRUE;{\ are there spaces?}
  157.     IF strpos(DirName,' ')>-1 THEN QuoteMode:=TRUE;{/ in the filename ?}
  158.  
  159.     StrCpyII(FinalName,CmdLine,0,Pos-1);
  160.     IF QuoteMode THEN StrCat(FinalName,"\"");
  161.     StrCat(FinalName,DirName);
  162.     I:=strlen(DirName)-1;
  163.     IF (DirName[I]<>'/') AND (DirName[I]<>':') THEN strcat(FinalName,"/");
  164.     StrCat(FinalName,BestName);
  165.     IF QuoteMode THEN StrCat(FinalName,"\""); {\" puts a " in the string}
  166.     StrCpyII(TempStr,CmdLine,Pos+1,strlen(CmdLine));
  167.     StrCat(FinalName,TempStr);
  168.     writeln(FinalName);
  169.     IF Execute(FinalName,Filehandle(0),DOSOutput) THEN;
  170.   END;
  171. END.
  172.